package com.bytedance.common.databinding;

import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.SparseArray;
import android.view.Choreographer;
import android.view.View;

import java.lang.ref.WeakReference;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.WeakHashMap;

public final class DataBinding extends a {
    interface t$a {
        e a(DataBinding arg1);
    }

    interface b {
        void a(Object arg1);

        void b(Object arg1);
    }

    final class c {
        Object a;
        int b;

        c(byte arg1) {
            this();
        }

        private c() {

        }
    }

    static final class d extends com.bytedance.common.databinding.l.a implements b {
        final e a;

        public d(DataBinding arg2) {

            this.a = new e(arg2, ((b) this));
        }

        public final void a(l arg4) {
            DataBinding v1 = this.a.b();
            if (v1 != null) {
                Object v0 = this.a.b;
                if ((((l) v0)) == arg4) {
                    DataBinding.a(v1, v0, 0);
                }
            }
        }

        public final void a(Object arg1) {
            ((l) arg1).addOnListChangedCallback(((com.bytedance.common.databinding.l.a) this));
        }

        public final void b(l arg1) {
            this.a(arg1);
        }

        public final void b(Object arg1) {
            ((l) arg1).removeOnListChangedCallback(((com.bytedance.common.databinding.l.a) this));
        }

        public final void c(l arg1) {
            this.a(arg1);
        }

        public final void d(l arg1) {
            this.a(arg1);
        }

        public final void e(l arg1) {
            this.a(arg1);
        }
    }

    static final class e extends WeakReference {
        final b a;
        Object b;

        public e(DataBinding arg1, b arg2) {
            super(arg1);
            this.a = arg2;
        }

        public final boolean a() {
            boolean v0 = false;
            if (this.b != null) {
                this.a.b(this.b);
                v0 = true;
            }

            this.b = null;
            return v0;
        }

        protected final DataBinding b() {
            Object v0 = this.get();
            if (v0 == null) {
                this.a();
            }

            return ((DataBinding) v0);
        }
    }

    static final class f extends com.bytedance.common.databinding.n.a implements b {
        final e a;

        public f(DataBinding arg2) {

            this.a = new e(arg2, ((b) this));
        }

        public final void a(Object arg1) {
        }

        public final void b(Object arg1) {
        }
    }

    static final class g extends com.bytedance.common.databinding.e.a implements b {
        final e a;

        public g(DataBinding arg2) {
            this.a = new e(arg2, ((b) this));
        }

        public final void a(com.bytedance.common.databinding.e arg3, int arg4) {
            DataBinding v1 = this.a.b();
            if (v1 != null && this.a.b == arg3) {
                DataBinding.a(v1, arg3, arg4);
            }
        }

        public final void a(Object arg1) {
            ((com.bytedance.common.databinding.e) arg1).addOnPropertyChangedCallback(((com.bytedance.common.databinding.e.a) this));
        }

        public final void b(Object arg1) {
            ((com.bytedance.common.databinding.e) arg1).removeOnPropertyChangedCallback(((com.bytedance.common.databinding.e.a) this));
        }
    }

    public final Runnable a;
    Set b;
    public boolean c;
    boolean d;
    HashMap<Object, List<q>> e;
    HashMap<Object, SparseArray<List<q>>> f;
    boolean g;
    final ArrayDeque h;
    public boolean i;
    private static final DataBinding.t$a j;
    private static final DataBinding.t$a k;
    private static final View.OnAttachStateChangeListener l;
    private WeakHashMap m;
    private final View n;
    private Choreographer o;
    private final Choreographer.FrameCallback p;
    private Handler q;

    static {
        j = new u();
        k = new v();
        new w();
        l = Build.VERSION.SDK_INT < 19 ? null : new y();
    }

    private DataBinding(View arg3) {
        this.a = new z(this);
        this.c = false;
        this.h = new ArrayDeque(8);
        this.m = new WeakHashMap(8);
        this.e = new HashMap(8);
        this.f = new HashMap(8);
        this.b = new HashSet(8);
        this.n = arg3;
        if (Looper.myLooper() == null) {
            throw new IllegalStateException("DataBinding must be created in view's UI Thread");
        }

        if (Build.VERSION.SDK_INT >= 16) {
            this.o = Choreographer.getInstance();
            this.p = new aa(this);
        } else {
            this.p = null;
            this.q = new Handler(Looper.myLooper());
        }

        arg3.setTag(com.ss.android.article.browser.R.id.p, this);
    }

    static Runnable a(DataBinding arg1) {
        return arg1.a;
    }

    public static DataBinding a(View arg1) {
        Object v0_1;
        if (arg1 != null) {
            v0_1 = arg1.getTag(com.ss.android.article.browser.R.id.p);
            if (v0_1 == null) {
                v0_1 = new DataBinding(arg1);
            }
        } else {
            v0_1 = new DataBinding(arg1);
        }

        return ((DataBinding) v0_1);
    }

    private void a(q arg6, Object[] arg7) {
        List v0_1;
        int v2 = arg7.length;
        int v1;
        for (v1 = 0; v1 < v2; ++v1) {
            Object v3 = arg7[v1];
            v0_1 = this.e.get(v3);
            if (v0_1 == null) {
                v0_1 = new ArrayList();
                this.e.put(v3, v0_1);
            }

            ((List) v0_1).add(arg6);
        }
    }

    static void a(DataBinding arg1, Object arg2, int arg3) {
        if (arg1.a(arg2, arg3)) {
            arg1.d();
        }
    }

    private boolean a(Object arg5, int arg6) {
        boolean v0_3;
        Object v0_1;
        if (!this.m.containsKey(arg5)) {
            v0_3 = false;
        } else if (this.f.containsKey(arg5)) {
            ArrayDeque v3 = this.h;
            synchronized (v3) {
                v0_1 = this.h.poll();
                if (v0_1 != null) {
                } else {
                    v0_1 = new c((byte) 0);
                }
            }

            ((c) v0_1).a = arg5;
            ((c) v0_1).b = arg6;
            this.b.add(v0_1);
            v0_3 = true;
        } else {
            this.b.add(arg5);
            v0_3 = true;
        }

        return v0_3;
    }

    static void a(List<q> arg2) {
        if (arg2 != null) {
            Iterator<q> v1 = arg2.iterator();
            while (v1.hasNext()) {
                v1.next().a();
            }
        }
    }

    private boolean a(Object arg4, DataBinding.t$a arg5) {
        boolean v0;
        if (arg4 == null) {
            v0 = false;
        } else {
            Object v0_1 = this.m.get(arg4);
            if (v0_1 == null) {
                this.b(arg4, arg5);
                v0 = true;
            } else if (((e) v0_1).b == arg4) {
                v0 = false;
            } else {
                v0_1 = this.m.get(arg4);
                if (v0_1 != null) {
                    ((e) v0_1).a();
                }

                this.b(arg4, arg5);
                v0 = true;
            }
        }

        return v0;
    }

    public final void a() {
        this.e.clear();
        this.f.clear();
    }

    public final void a(q arg5, com.bytedance.common.databinding.e[] arg6) {
        this.f();
        this.a(arg5, ((Object[]) arg6));
        synchronized (this) {
            int v1 = arg6.length;
            int v0_1;
            for (v0_1 = 0; v0_1 < v1; ++v0_1) {
                com.bytedance.common.databinding.e v2 = arg6[v0_1];
                this.a(v2, DataBinding.j);
                this.b.add(v2);
            }
        }
    }

    public final void a(q arg4, l[] arg5) {
        int v0 = 0;
        this.f();
        this.a(arg4, ((Object[]) arg5));
        synchronized (this) {
            while (true) {
                if (v0 > 0) {
                    return;
                }
                try {
                    l v1 = arg5[0];
                    this.a(v1, DataBinding.k);
                    this.b.add(v1);
                    ++v0;
                    continue;
                } catch (Throwable v0_1) {
                    break;
                }
            }
        }
    }

    private void b(Object arg3, DataBinding.t$a arg4) {
        e v0_1;
        if (arg3 != null) {
            v0_1 = (DataBinding.e) this.m.get(arg3);
            if (v0_1 == null) {
                v0_1 = arg4.a(this);
                this.m.put(arg3, v0_1);
            }

            v0_1.a();
            v0_1.b = arg3;
            if (v0_1.b == null) {
                return;
            }

            v0_1.a.a(v0_1.b);
        }
    }

    static boolean b(DataBinding arg1) {
        arg1.c = false;
        return false;
    }

    final boolean b() {
        synchronized (this) {
            boolean v0_1 = !this.b.isEmpty() ? true : false;
            return v0_1;
        }
    }

    static View c(DataBinding arg1) {
        return arg1.n;
    }

    public final void c() {
        Iterator v1 = this.m.values().iterator();
        while (v1.hasNext()) {
            Object v0 = v1.next();
            if (v0 == null) {
                continue;
            }

            ((e) v0).a();
        }

        this.a();
    }

    final void d() {
        synchronized (this) {
            if (this.c) {
                return;
            } else {
                this.c = true;
            }
        }

        if (Build.VERSION.SDK_INT >= 16) {
            this.o.postFrameCallback(this.p);
        } else {
            this.q.post(this.a);
        }

        return;
    }

    static View.OnAttachStateChangeListener e() {
        return DataBinding.l;
    }

    private void f() {
        if (!this.i) {
            throw new IllegalStateException("Must call startBinding() at first.");
        }
    }

    protected final void finalize() throws Throwable {
        this.c();
    }
}

